package com.cheboohui.iqs.taiping.direct.price;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.spring.entity.Price;
import com.cheboohui.iqs.spring.entity.price.request.PriceRequest;
import com.cheboohui.iqs.spring.entity.price.response.PriceResult;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;
import com.cheboohui.iqs.taiping.direct.order.CarListGet;
import com.cheboohui.iqs.taiping.direct.order.GetOrderNo;

/**
 * 太平报价
 * 
 * @author xielonghai
 *
 */
public class PriceTaiPing {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(PriceTaiPing.class);

	public static PriceResult price(PriceRequest priceRequest) {
		JSONObject owner = getOwner(priceRequest);

		JSONObject obj = new JSONObject();
		obj.put("session", "");// 太平接口回话session
		obj.put("orderNo", "");// 订单号

		log.info(Config.value + "--进入车险报价......第一步获取订单号...");
		/******* 获取订单号 *********/
		try {
			owner.put("direct", false);// 跳过E02接口，走E05-->E04报价
			owner.put("cityCode", "");// 不取城市代码，跳过E01请求接口
			owner.put("session", "");

			ResTaiPing toNo = GetOrderNo.orderNo(owner);
			if (!toNo.isState()) {
				PriceResult c05 = new PriceResult(toNo.getError(), toNo.getMsg());
				if (toNo.getError().equals(ErrorNews.C05)) {
					c05.setResult(new Price(CarListGet.getCars(toNo.getResult().getJSONArray("vehicle"))));
					return c05;
				} else
					return c05;
			} else {
				obj.put("session", toNo.getResult().getString("session"));
				obj.put("orderNo", toNo.getResult().getString("orderNo"));
			}
		} catch (Exception e) {
			log.error(Config.value + "--车险报价第一步获取订单号出现异常:" + e);
			return new PriceResult(ErrorNews.L03, "车险报价第一步获取订单号出现异常:" + e.getMessage());
		}

		log.info(Config.value + "--进入车险报价......第二步,报价方式V02...session:" + obj.getString("session") + ",orderNo:"
				+ obj.getString("orderNo"));
		try {
			PriceStep price = new PriceStep(priceRequest, obj);

			log.info(Config.value + "--进入车险报价......第二步,报价方式V02...session:" + obj.getString("session") + ",orderNo:"
					+ obj.getString("orderNo") + ",报价以及结果返回处理结束....");
			return price.price();
		} catch (Exception e) {
			log.error(Config.value + "--进入车险报价......第二步,报价方式V02出现异常:" + e);
			return new PriceResult(ErrorNews.L03, "车险报价第二步,报价方式V02出现异常:" + e.getMessage());
		}
	}

	/**
	 * 车主信息
	 * 
	 * @param priceRequest
	 * @return
	 */
	private static JSONObject getOwner(PriceRequest priceRequest) {
		JSONObject owner = new JSONObject();
		owner.put("name", priceRequest.getLicense().getName());// 车主名称--Y
		// owner.put("identifyNumber", getPara(p, "ownerIDcard"));// 车主身份证号
		owner.put("chgOwnerFlag", priceRequest.getLicense().getTransfer());// 是否过户车-00、非过户车，01投保过户
		if (!StringUtil.isNulls(priceRequest.getLicense().getTransferDate()))
			owner.put("transferDate", priceRequest.getLicense().getTransferDate());// 过户日期-若是过户车，需传值；
		else
			owner.put("transferDate", "");// 过户日期-若是过户车，需传值；
		owner.put("licenseNo", priceRequest.getLicense().getLicenceNo());// 车牌号--Y
		owner.put("engineNo", priceRequest.getLicense().getEngineNo());// 发动机号--Y
		owner.put("frameNo", priceRequest.getLicense().getFrameNo());// 车架号
		owner.put("SingeinDate", priceRequest.getLicense().getRegisteredTime());// 登记日期--Y

		owner.put("brandName", priceRequest.getLicense().getLabelNo());
		owner.put("purchase", priceRequest.getCar().getPurchase());

		return owner;
	}

}
